<template>
  <div>
    <template v-for="(item, index) in options">
      <template v-if="values.includes(item.value)">
        <span class="ml-1 mr-1">
            <span
              v-if="(item.elTagType === 'default' || item.elTagType === '') && (item.elTagClass === '' || item.elTagClass == null)"
              :key="item.value"
              :index="index"
              :class="item.elTagClass">
          {{ item.label + ' ' }}
        </span>
        <el-tag v-else-if="item.elTagType==='primary'" :key="item.value + ''" :index="index"
                type="primary">
        <template #default>
      <svg-icon v-if="item.icon" :icon-class=" item.icon" />
      {{ item.label + ' ' }}
    </template>
  </el-tag>
  <a-tag v-else :key="item.value + ''" :index="index"
         :color="tagTypeOptions.includes(item.elTagType)? item.elTagType : 'processing'"
         :bordered="item.tagBordered ? item.tagBordered : true"
  >
    <template #icon>
      <svg-icon v-if="item.icon" :icon-class="item.icon" />
    </template>
    {{ item.label + ' ' }}
  </a-tag>
</span>
      </template>
    </template>
    <template v-if="unmatch && showValue">
      {{ unmatchArray }}
    </template>

  </div>
</template>

<script setup lang="ts">
interface Props {
  options: Array<DictDataOption>;
  value: number | string | Array<number | string>;
  showValue?: boolean;
  separator?: string;
  size?: 'large' | 'default' | 'small';
}

const props = withDefaults(defineProps<Props>(), {
  showValue: true,
  separator: ',',
  size: 'default'
});
const tagTypeOptions = ref(['pink', 'red', 'orange', 'green', 'cyan', 'blue', 'purple', 'default',
  'processing', 'success', 'error', 'warning', 'magenta', 'volcano', 'gold', 'lime', 'geekblue']);
const values = computed(() => {
  if (props.value === '' || props.value === null || typeof props.value === 'undefined') return [];
  return Array.isArray(props.value) ? props.value.map((item) => '' + item) : String(props.value).split(props.separator);
});

const unmatch = computed(() => {
  if (props.options?.length == 0 || props.value === '' || props.value === null || typeof props.value === 'undefined') return false;
  // 传入值为非数组
  let unmatch = false; // 添加一个标志来判断是否有未匹配项
  values.value.forEach((item) => {
    if (!props.options.some((v) => v.value === item)) {
      unmatch = true; // 如果有未匹配项，将标志设置为true
    }
  });
  return unmatch; // 返回标志的值
});

const unmatchArray = computed(() => {
  // 记录未匹配的项
  const itemUnmatchArray: Array<string | number> = [];
  if (props.value !== '' && props.value !== null && typeof props.value !== 'undefined') {
    values.value.forEach((item) => {
      if (!props.options.some((v) => v.value === item)) {
        itemUnmatchArray.push(item);
      }
    });
  }
  // 没有value不显示
  return handleArray(itemUnmatchArray);
});

const handleArray = (array: Array<string | number>) => {
  if (array.length === 0) return '';
  return array.reduce((pre, cur) => {
    return pre + ' ' + cur;
  });
};
</script>

<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
</style>
